﻿package com.neriksworkshop.colourLoversDemos.colourLoversBrowser.view 
{
	import com.neriksworkshop.lib.colourLovers.items.IGraphicItem;
	import com.neriksworkshop.lib.colourLovers.items.IItem;
	import com.neriksworkshop.lib.colourLovers.params.Params;
	import com.neriksworkshop.lib.colourLovers.requests.IGraphicRequest;
	import com.neriksworkshop.lib.colourLovers.requests.params.OrderCol;
	import com.neriksworkshop.lib.colourLovers.requests.params.SortBy;
	import fl.controls.CheckBox;
	import fl.controls.ComboBox;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fl.controls.Button;	
	import com.neriksworkshop.lib.colourLovers.requests.params.Format
	import com.neriksworkshop.lib.colourLovers.requests.IRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author Erik
	 */
	public class MainView extends MovieClip
	{
		
		
		private var _tabs:/*Button*/Array;
		private var _views:/*MovieClip*/Array;
		
		private var _currentViewIndex:int = 0;
		private var _currentView:MovieClip;
		
		private var _currentDataObj:Object;
		
		private var _currentResTabIsGraphic:Boolean = true;
		
		private var _results:Sprite;
		private var _resultsGraphic:Sprite;
		private var _resultsText:Sprite;
		private var _resultsTextTf:TextField;
		
		
		public function MainView() 
		{
			build();
		}
		
		
		//------------------ public methods ----------------------------------
		
		public function get currentDataObj():Object { return _currentDataObj; }

		public function showResults(req:IRequest)
		{
			//req.items, String(req.xmlData), type
			_resultsTextTf.text = String(req.xmlData);
			
			var itemY:Number = 0;
			
			//_results.removeChild(_resultsGraphic);
			_resultsGraphic = new Sprite();
			//_results.addChild(_resultsGraphic);
			
			for each (var item:IItem in req.items) 
			{
				var itemSp:Sprite;
				
				if (item is IGraphicItem)
				{
					itemSp = (item as IGraphicItem).createSprite();
				}
				
				itemSp.y = itemY;
				itemY += Params.DEFAULT_DIMENSIONS.height + 4;
				
				_resultsGraphic.addChild(itemSp);
			}
		
			
			sp.source =  (_currentResTabIsGraphic) ? _resultsGraphic : _resultsText;
			sp.update();
		}
		
		
		
		
		
		
		//------------------ private methods ----------------------------------
		
		private function build():void
		{
			_tabs = [colors, palettes, patterns, lovers, stats];
			_views = [viewColors, viewPalettes, viewPatterns];
			_currentView = viewColors;

			
			//tabs --------------
			_tabs[0].emphasized = true;
			
			for (var i:int = 0; i < _tabs.length; i++) 
			{
				_tabs[i].addEventListener(MouseEvent.CLICK, onTabClick);
			}
			
			//views ------------------
			for (var j:int = 0; j < _views.length; j++) 
			{
				if (j > 0) _views[j].visible = false;
				
				for (var k:int = 0; k < OrderCol.LIST.length; k++) 
				{
					(_views[j].orderCol.orderCol as ComboBox).addItem( { label:OrderCol.LIST[k] } );
				}
				
				(_views[j].orderCol.sortBy as ComboBox).addItem( { label:SortBy.ASC } );
				(_views[j].orderCol.sortBy as ComboBox).addItem( { label:SortBy.DESC } );
				
				
				
			}
			
			
			//submit / submit optns ------------------------
			search.addEventListener(MouseEvent.CLICK, onSubmit);			
			for each (var formatStr:String in Format.LIST)  { format.addItem( { label:formatStr } ) }
			
			
			//result tabs
			resGraphic.addEventListener(MouseEvent.CLICK, onResultTabClick);
			resText.addEventListener(MouseEvent.CLICK, onResultTabClick);
			
			
			//results panel
			_results = new Sprite();
			_resultsGraphic = new Sprite();
			//_results.addChild(_resultsGraphic);
			_resultsText = new Sprite();
			//_results.addChild(_resultsText);
			_resultsText.visible = false;
			_resultsTextTf = new TextField();
			_resultsTextTf.width = 470;
			_resultsTextTf.autoSize = TextFieldAutoSize.LEFT;
			_resultsTextTf.wordWrap = true;
			_resultsTextTf.defaultTextFormat = new TextFormat("_typewriter", 10, 0x0);
			_resultsText.addChild(_resultsTextTf);
			addChild(_results);
			
			
			
		}

		
		private function onTabClick(e:MouseEvent):void 
		{
			var tab:Button = e.target as Button;
			
			for (var i:int = 0; i < _views.length; i++) 
			{
				var view:MovieClip = _views[i];
				
				if (tab.label == view.name.substring(4))
				{
					tab.emphasized = true;
					view.visible = true
					_currentViewIndex = i;
					_currentView = view;
				}
				else
				{
					_tabs[i].emphasized = false;
					view.visible = false;
				}
			}
		}
		
		
		private function onResultTabClick(e:MouseEvent):void 
		{
			if (e.target == resGraphic)
			{
				_currentResTabIsGraphic  = true;
				resGraphic.emphasized = true;
				_resultsGraphic.visible = true;
				resText.emphasized = false;
				_resultsText.visible = false;
				sp.source = _resultsGraphic;
			}
			else
			{
				_currentResTabIsGraphic = false;
				resGraphic.emphasized = false;
				_resultsGraphic.visible = false;
				resText.emphasized = true;
				_resultsText.visible = true;
				sp.source = _resultsText;
			}
		}		
		
		
		private function onSubmit(e:MouseEvent):void 
		{
			var dataObj:Object = { };
			
			dataObj.numResults = numResults.value;
			dataObj.resultOffset = resultOffset.value;
			dataObj.format = format.selectedLabel;
			
			
			if (_currentViewIndex == 0 || _currentViewIndex == 1 || _currentViewIndex == 2)
			{
				
				//Color+Palette+Pattern
				if (_currentView.lover.active.selected)			dataObj.lover = _currentView.lover.lover.text;
				if (_currentView.keywords.active.selected)		
				{
					dataObj.keywords = _currentView.keywords.keywords.text;
					dataObj.keywordExact = _currentView.keywords.keywordExact.selected;
				}					
				if (_currentView.orderCol.active.selected)
				{
					dataObj.orderCol = _currentView.orderCol.orderCol.selectedLabel;
					dataObj.sortBy = _currentView.orderCol.sortBy.selectedLabel;
				}
				
				
				
				if (_currentViewIndex == 1 || _currentViewIndex == 2)
				{
					//Palette+Pattern
					
					if (_currentView.hueOption.active.selected)		
					{
						dataObj.hueOption = new Array();
						var hueOptionCbs:Array = [_currentView.hueOption.red, _currentView.hueOption.orange, _currentView.hueOption.yellow, _currentView.hueOption.green, _currentView.hueOption.aqua, _currentView.hueOption.blue, _currentView.hueOption.violet, _currentView.hueOption.fuchsia];
						for each (var cb:CheckBox in hueOptionCbs)  { if (cb.selected) dataObj.hueOption.push(cb.name); }
					}
					if (_currentView.hex.active.selected)			dataObj.hex = _currentView.hex.hex.text;					
					
					
					if (_currentViewIndex == 1)
					{
						//Palette
						dataObj.type = "Palette";
						if (_currentView.showPaletteWidths.active.selected) dataObj.showPaletteWidths = _currentView.showPaletteWidths.showPaletteWidths.selected;
					}
					else
					{
						//Pattern
						dataObj.type = "Pattern";
					}
				}
				
				else
				{
					//Color
					dataObj.type = "Color";
					if (_currentView.hueRange.active.selected)		dataObj.hueRange = [_currentView.hueRange.hueRangeMin.value, _currentView.hueRange.hueRangeMax.value];
					if (_currentView.briRange.active.selected)		dataObj.briRange = [_currentView.briRange.briRangeMin.value, _currentView.briRange.briRangeMax.value];
				}
				
			}

			
			_currentDataObj = dataObj;
			
			dispatchEvent(new Event("submit"));
		}
		
		
		
		
	}
	
}